Shell custom decompression Linux: RedHatEnterpriseLinux5 writes a script named smartzip. sh, which can automatically decompress bzip2, gzip, and zip compressed files: smartzip. shJs code #! /Bin/bashftype quot; $ (file quot; $1 quot;) quot ;... linux: Red Hat Enterprise Linux 5 writes a file named smartzip. sh script, which can automatically decompress bzip2,
Linux: Red Hat Enterprise Linux 5 writes a file named smartzip. sh script, which can automatically decompress bzip2, gzip, and zip compressed files: smartzip. sh Js Code #! /Bin/bash ftype = "$ (file" $1 ")" case "$ ftype" in "$1: Zip archive" *) unzip "$1 ";; "$1: gzip compressed" *) gunzip "$1"; "$1: bzip2 compressed" *) bunzip2 "$1 ";;*) echo "File $1 can not be uncompressed with
will return:
Lf.gz:gzip compressed data, deflated, original filename,
Last Modified:mon Aug 23:09:18 2001, Os:unix
We use this to write a script called Smartzip, which automatically extracts bzip2, gzip, and zip-type compressed files:
#!/bin/sh
Ftype= ' file '
Case "$ftype" in
"$1:zip Archive" *)
Unzip "$";;
"$1:gzip Compressed" *)
Gunzip "$";;
"$1:bzip2 Compressed" *)
Bunzip2 "$";;
*) echo "File of Can not is uncompressed with
Scripting is essential for Linux testing, and the name of the shell script can be arbitrarily defined, and no suffix, such as the name can be written abc,smartzip, as long as the runtime typeThe./smartzip will be able to run the script.Do not use spaces at the beginning of each command line.Part 1th. Basics of writing Linux scripts1.1 Basic Introduction to grammar1.1.1 OpeningThe program must start with the
identify the file type of a given file, for example, file lf.gz. The output result is:
Lf.gz: gzip compressed data, deflated, original filename, last modified: Mon Aug 27 23:09:18 2001, OS: Unix
We use this to write a script named smartzip, which can automatically decompress Bzip2, Gzip, and zip compressed files:
#! /Bin/sh FTYPE = 'file "$1" '# Note' and 'is different case "$ FTYPE" in "$1: ZIP Archive "*) unzip "$1"; "$1: gzip COMPRESSED" *)
effect, print the error message and exit the script. Note that the following two commands must be used:
-Print the error message.
-Exit the program.
We use curly braces to put the two commands together as one command in the form of an anonymous function. The common function will be explained later. We can use the if expression to accomplish anything without the use of and or operators, but the use of and or operators is much more convenient.
Case statement
The case expression
will return:
Lf.gz: gzip compressed data, deflated, original filename,
Last modified: Mon Aug 27 23:09:18 2001, OS: Unix
We use this to write a script named smartzip, which can automatically decompress bzip2, gzip, and zip compressed files:
#! /Bin/sh
Ftype = 'file "$1 "'
Case "$ ftype" in
"$1: Zip archive "*)
Unzip "$1 ";;
"$1: gzip compressed "*)
Gunzip "$1 ";;
"$1: bzip2 compressed "*)
Bunzip2 "$1 ";;
*) Echo "File $1 can not be uncompressed with
, for example: file lf.gz This will return: lf.gz: gzip compressed data, deflated, original filename, last modified: Mon Aug 27 23:09:18 2001, OS: Unix we used this to write a script called smartzip, which can automatically decompress Bzip2, gzip and zip compressed files: #! /Bin/sh FTYPE = 'file "$1" ' case "$ FTYPE" in "$1: ZIP Archive "*) unzip "$1"; "$1: gzip COMPRESSED" *) gunzip "$1"; "$1: bzip2 COMPRESSED "*) bunzip2" $1 "; *) ech
operator, but it is much more convenient to use the sum or operator.2. caseCase: The expression can be used to match a given string, not a number.
case ... in...) do something here ;;esac
Let's look at an example. The file command can identify the file type of a given file, for example:File lf.gzThis will return:
lf.gz: gzip compressed data, deflated, original filename,last modified: Mon Aug 27 23:09:18 2001, os: Unix
We use this to write a script named sma
anything without the sum or operator, but it is much more convenient to use the sum or operator.
A case expression can be used to match a given string, not a number.
Case... in
...) Do something here ;;
Esac
Let's look at an example. The file command can identify the file type of a given file, for example:
File lf.gz
This will return:
Lf.gz: gzip compressed data, deflated, original filename,
Last modified: Mon Aug 27 23:09:18 2001, OS: Unix
We use this to write a script named
The case expression can be used to match a given string, not a number (do not confuse switch... case in C ).
Case... in...) do something here esac
The file command can identify the file type of a given file, for example, file lf.gz. The output result is:
Lf.gz: gzip compressed data, deflated, original filename, last modified: Mon Aug 27 23:09:18 2001, OS: Unix
We use this to write a script named smartzip, which can automatically decompress Bzip2,
here ;;
Esac
Let's look at an example. The file command can identify the file type of a given file, for example:
File lf.gz
This will return:
Lf.gz: gzip compressed data, deflated, original filename,
Last modified: Mon Aug 27 23:09:18 2001, OS: Unix
We use this to write a script named smartzip, which can automatically decompress bzip2, gzip, and zip compressed files:
#! /Bin/sh
Ftype = 'file "$1 "'
Case "$ ftype" in
"$1: Zip archive "*)
Unzip "$1 ";;
:
File lf.gz
This will return:
Lf.gz:gzip compressed data, deflated, original filename,
Last Modified:mon 23:09:18 2001, Os:unix
We use this to write a script called Smartzip, which automatically extracts compressed files of bzip2, gzip, and zip types:
#!/bin/sh
Ftype= ' file ' $ "'
Case ' $ftype ' in
"$1:zip Archive" *)
Unzip "$";;
"$1:gzip Compressed" *)
Gunzip "$";;
"$1:bzip2 Compressed" *)
Bunzip2 "$";;
*) Error "File" can not is uncompressed with
files for a given file, such as:
File lf.gz
This will return:
Lf.gz:gzip compressed data, deflated, original filename,
Last Modified:mon 23:09:18 2001, Os:unix
We use this to write a script called Smartzip, which automatically extracts compressed files of bzip2, gzip, and zip types:
#!/bin/sh
Ftype= ' file ' $ "'
Case ' $ftype ' in
"$1:zip Archive" *)
Unzip "$";;
"$1:gzip Compressed" *)
Gunzip "$";;
"$1:bzip2 Compressed" *)
Bunzip2 "$";;
*) Error "Fi
:
-Print the error message.
-Exit the program.
We use curly braces to put the two commands together as one command in the form of an anonymous function. The common function will be explained later. We can use the if expression to accomplish anything without the use of and or operators, but the use of and or operators is much more convenient.
[Edit]
Case statement
The case expression can be used to match a given string, not a number (do not co
curly braces to put the two commands together as one command in the form of an anonymous function. General functions will be mentioned below.
We can use the if expression to do anything without the sum or operator, but it is much more convenient to use the sum or operator.
A case expression can be used to match a given string, not a number.
Case... in...) Do something here ;;Esac
Let's look at an example. The file command can identify the file type of a given file, for example:
File lf.gz
This
mailfolder is readable. If it is readable, it prints a line of "from" in the file. If it is not readable or the operation takes effect, print the error message and exit the script. There is a problem here, that is, we must have two commands:-Print the error message.-Exit the program.We use curly braces to put the two commands together as one command in the form of an anonymous function. General functions will be mentioned below. We can use the if expression to do anything without the sum or ope
readable or the operation takes effect, print the error message and exit the script. Note that the following two commands must be used:
-Print the error message.
-Exit the program.
We use curly braces to put the two commands together as one command in the form of an anonymous function. The common function will be explained later. We can use the if expression to accomplish anything without the use of and or operators, but the use of and or operators is much more convenient.3. Case statement
The
smartzip, which can automatically decompress bzip2, gzip, and zip compressed files:
#! /Bin/sh
Ftype = 'file "$1 "'
Case "$ ftype" in
"$1: Zip archive "*)
Unzip "$1 ";;
"$1: gzip compressed "*)
Gunzip "$1 ";;
"$1: bzip2 compressed "*)
Bunzip2 "$1 ";;
*) Echo "File $1 can not be uncompressed with smartzip ";;
Esac
You may notice that we use a special variable $1 here. The variable contains the first paramet
write a script named smartzip, which can automatically decompress Bzip2, Gzip, and zip compressed files:
#!/bin/bash ftype="$(file "$1")" case "$ftype" in "$1: Zip archive"*) unzip "$1";; "$1: gzip compressed"*) gunzip "$1";; "$1: bzip2 compressed"*) bunzip2 "$1";; *) echo "File $1 can not be uncompressed with smartzip";; esac
You may notice that a special variable $1 is used above, which contain
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.